/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Dec 20, 2011  2:40:23 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : sap.jco.server
 * File Name : SAPServer.java
 * TODO
 */
package sap.jco.server;

import org.apache.log4j.Logger;
import com.sap.mw.jco.IRepository;
import com.sap.mw.jco.JCO;

/**
 * @author Chatree Siangklom of Administrator
 * Date =Nov 19, 2013 : 5:05:56 PM 
 * CreateBy=Chatree Siangklom
 * SAPServer
 * TODO
 */
public class SAPServer {
	
	private static final Logger log = Logger.getLogger(SAPServer.class);	
	static ServerProvider serverConnections[] = new ServerProvider[SAPServerInfo.MaxConnection()];

	/**
	 * Start Server
	 * Date =Nov 19, 2013 : 5:06:31 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 */
    public static void startServers()  
    {
      JCO.addClientPool(SAPServerInfo.PoolName(), 
		  				SAPServerInfo.MaxConnection(), 
		  				SAPServerInfo.Client(), 
		  				SAPServerInfo.User(), 
		  				SAPServerInfo.Password(),
		  				SAPServerInfo.Language(), 
		  				SAPServerInfo.Host(), 
		  				SAPServerInfo.Port());
      
      IRepository repository = JCO.createRepository(SAPServerInfo.RepositoryName(), SAPServerInfo.PoolName());
      for(int i = 0; i < serverConnections.length; i++) 
      {
          serverConnections[i] = new ServerProvider 
                             (SAPServerInfo.Host(),  		//gateway host, often the same as host
                              SAPServerInfo.GatewayService(), 	//gateway service, generally sapgw+<SYSNR>
                              SAPServerInfo.ProgramId(),			//corresponds to program ID defined in SM59
                              SAPServerInfo.isUnicode(), 			//Unicode defined in SM59
                              repository);
          serverConnections[i].start();
           
          log.info("Start Servers i : " + i + " Server Connections State : " +  serverConnections[i].getState());
          System.out.println("Start Servers i : " + i + " Server Connections State : " +  serverConnections[i].getState());
          
//           try {
//        	   
//				if(serverConnections[i].conn_normal.isClosed())
//					log.info("serverConnections ["+ i +"].conn_normal Closed.");
//				else 
//					log.info("serverConnections ["+ i +"].conn_normal Opening.");
//				
//           }catch (SQLException e) {
//        	   log.error(e);
//           }
       }
    }
    
    /**
     * stop the Servers
     */
    public static void stopServers() {
       for(int i = 0; i < serverConnections.length; i++) 
       {    	   
           serverConnections[i].stop();
           log.info("Stop Servers i : " + i);
           System.out.println("Stop Servers i : " + i);
       }
    }
    
	/**
	 * Date =Nov 19, 2013 : 5:06:17 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @param args
	 */
	public static void main(String[] args) {
		startServers();
	}
}
